package com.ulic.customerManApplication.util.imagecode;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;

public class ImageOut {

	private static final int WIDTH = 100;
	private static final int HEIGHT = 25;

	private static Color getRandomColor() {
		return new Color(50, 50,50);
	}


	private static Font getRandomFont() {
		return new Font("Times New Roman", Font.PLAIN,25);
	}

	public static char getRandomChar() {
		Random random = new Random();
		if (random.nextInt(5) < 3) {
			return (char) (65 + random.nextInt(26));
		}
		return (char) (65 + random.nextInt(26));

	}

	public static Pair<String, BufferedImage> getImage() {

		BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g = image.createGraphics();
		g.setColor(new Color(255,245, 255));
		g.fillRect(0, 0, WIDTH, HEIGHT);
		StringBuilder s = new StringBuilder();
		for (int i = 0; i < 4; i++) {
			String t = String.valueOf(getRandomChar());
			s.append(t);
			g.setColor(getRandomColor());
			g.setFont(getRandomFont());
			g.drawString(t, 3 + 23 * i, 22);
		}
		g.dispose();
		return new Pair<String, BufferedImage>(s.toString(), image);
	}
}